package com.yanxp.mybatis.mapper;

import com.yanxp.mybatis.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserMapper {

    @Insert("insert into user(username) values(#{username})")
    void addUser(User user);

    @Update("update user set username=#{username} where id = #{id} ")
    void updateUser(User user);

    @Select("select id,username from user where id = #{id}")
    User selectOne(Integer id);

    @Select("select id,username from user")
    List<User> selectAll();

    @Delete("delete from user where id = #{id}")
    void deleteUser(Integer id);

    @Results(id = "userResults",value={
          @Result(property = "id",column = "id"),
          @Result(property = "username",column = "username")
    })
    @Select("select id,username from user where username like '%' #{username} '%'")
    List<User> selectUserByUsername(@Param("username") String username);


    @Results(id = "userOrderResults",value = {
            @Result(property = "id",column = "id"),
            @Result(property = "username",column = "username"),
            @Result(property = "orders",column = "id",javaType = List.class,
                    many =@Many(select = "com.yanxp.mybatis.mapper.OrderMapper.selectOne") )
    })
    @Select("select * from user")
    List<User> findUserOrders();
}
